package com.yoso.thread;

import cn.hutool.core.thread.ThreadUtil;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Notify {

    final Object lock = new Object();
    @Test
    public void t1() {
        int num = 8;
        ExecutorService executorService = Executors.newFixedThreadPool(num);
        for (int i = 0; i < num; i++) {
            int finalI = i;
            executorService.submit(() -> doSome(finalI));

        }
        ThreadUtil.sleep(2000);

    }

    @SneakyThrows
    public void doSome(int id) {
        if (id == 6) {
            ThreadUtil.sleep(500);
        }
        synchronized (lock) {
            // if (id == 2) {
            //     System.out.println("==================    " + id + "  wait");
            //     lock.wait();
            // }
            // if (id == 4) {
            //     System.out.println("==================    " + id + "  wait");
            //     lock.wait();
            // }
            if (id == 6) {
                System.out.println(id + "  notify");
                lock.notifyAll();
            }

            ThreadUtil.sleep(100);
            System.out.println(id + "  do");
        }
    }


}
